﻿using System;
using System.Text;
using GeoAPI.Geometries;

namespace NetTopologySuite.GeometriesGraph
{
	public class Label
	{
		public static Label ToLineLabel(Label label)
		{
			Label label2 = new Label(Location.Null);
			for (int i = 0; i < 2; i++)
			{
				label2.SetLocation(i, label.GetLocation(i));
			}
			return label2;
		}

		public Label(Location onLoc)
		{
			this.elt[0] = new TopologyLocation(onLoc);
			this.elt[1] = new TopologyLocation(onLoc);
		}

		public Label(int geomIndex, Location onLoc)
		{
			this.elt[0] = new TopologyLocation(Location.Null);
			this.elt[1] = new TopologyLocation(Location.Null);
			this.elt[geomIndex].SetLocation(onLoc);
		}

		public Label(Location onLoc, Location leftLoc, Location rightLoc)
		{
			this.elt[0] = new TopologyLocation(onLoc, leftLoc, rightLoc);
			this.elt[1] = new TopologyLocation(onLoc, leftLoc, rightLoc);
		}

		public Label(int geomIndex, Location onLoc, Location leftLoc, Location rightLoc)
		{
			this.elt[0] = new TopologyLocation(Location.Null, Location.Null, Location.Null);
			this.elt[1] = new TopologyLocation(Location.Null, Location.Null, Location.Null);
			this.elt[geomIndex].SetLocations(onLoc, leftLoc, rightLoc);
		}

		public Label(Label lbl)
		{
			this.elt[0] = new TopologyLocation(lbl.elt[0]);
			this.elt[1] = new TopologyLocation(lbl.elt[1]);
		}

		public void Flip()
		{
			this.elt[0].Flip();
			this.elt[1].Flip();
		}

		public Location GetLocation(int geomIndex, Positions posIndex)
		{
			return this.elt[geomIndex].Get(posIndex);
		}

		public Location GetLocation(int geomIndex)
		{
			return this.elt[geomIndex].Get(Positions.On);
		}

		public void SetLocation(int geomIndex, Positions posIndex, Location _location)
		{
			this.elt[geomIndex].SetLocation(posIndex, _location);
		}

		public void SetLocation(int geomIndex, Location _location)
		{
			this.elt[geomIndex].SetLocation(Positions.On, _location);
		}

		public void SetAllLocations(int geomIndex, Location _location)
		{
			this.elt[geomIndex].SetAllLocations(_location);
		}

		public void SetAllLocationsIfNull(int geomIndex, Location _location)
		{
			this.elt[geomIndex].SetAllLocationsIfNull(_location);
		}

		public void SetAllLocationsIfNull(Location _location)
		{
			this.SetAllLocationsIfNull(0, _location);
			this.SetAllLocationsIfNull(1, _location);
		}

		public void Merge(Label lbl)
		{
			for (int i = 0; i < 2; i++)
			{
				if (this.elt[i] == null && lbl.elt[i] != null)
				{
					this.elt[i] = new TopologyLocation(lbl.elt[i]);
				}
				else
				{
					this.elt[i].Merge(lbl.elt[i]);
				}
			}
		}

		private void SetGeometryLocation(int geomIndex, TopologyLocation tl)
		{
			if (tl == null)
			{
				return;
			}
			this.elt[geomIndex].SetLocations(tl);
		}

		public int GeometryCount
		{
			get
			{
				int num = 0;
				if (!this.elt[0].IsNull)
				{
					num++;
				}
				if (!this.elt[1].IsNull)
				{
					num++;
				}
				return num;
			}
		}

		public bool IsNull(int geomIndex)
		{
			return this.elt[geomIndex].IsNull;
		}

		public bool IsAnyNull(int geomIndex)
		{
			return this.elt[geomIndex].IsAnyNull;
		}

		public bool IsArea()
		{
			return this.elt[0].IsArea || this.elt[1].IsArea;
		}

		public bool IsArea(int geomIndex)
		{
			return this.elt[geomIndex].IsArea;
		}

		public bool IsLine(int geomIndex)
		{
			return this.elt[geomIndex].IsLine;
		}

		public bool IsEqualOnSide(Label lbl, int side)
		{
			return this.elt[0].IsEqualOnSide(lbl.elt[0], side) && this.elt[1].IsEqualOnSide(lbl.elt[1], side);
		}

		public bool AllPositionsEqual(int geomIndex, Location loc)
		{
			return this.elt[geomIndex].AllPositionsEqual(loc);
		}

		public void ToLine(int geomIndex)
		{
			if (this.elt[geomIndex].IsArea)
			{
				this.elt[geomIndex] = new TopologyLocation(this.elt[geomIndex].GetLocations()[0]);
			}
		}

		public override string ToString()
		{
			StringBuilder stringBuilder = new StringBuilder();
			if (this.elt[0] != null)
			{
				stringBuilder.Append("A:");
				stringBuilder.Append(this.elt[0]);
			}
			if (this.elt[1] != null)
			{
				stringBuilder.Append(" B:");
				stringBuilder.Append(this.elt[1]);
			}
			return stringBuilder.ToString();
		}

		private readonly TopologyLocation[] elt = new TopologyLocation[2];
	}
}
